package com.fanhang.samples.spring.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fanhang.samples.spring.web.po.User;

/**
 * RestController 相比于 Controller，每个RequestMapping默认ResponseBody
 */
@Controller
@RequestMapping("request")
public class RequestController {

	@RequestMapping(value = "/", method = { RequestMethod.GET })
	public Map<String, Object> index() {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", "hello，this is home/index!");
		// 默认页面 home/index
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/test1", method = { RequestMethod.GET })
	public Map<String, Object> test1() {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", "hello");
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/test2", method = { RequestMethod.GET })
	public Map<String, Object> test2(int id) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("id", id);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/test3", method = { RequestMethod.GET })
	public Map<String, Object> test3(Integer id) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("id", id);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/test4", method = { RequestMethod.GET })
	public Map<String, Object> test4(Integer id, String name) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("id", id);
		result.put("name", name);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/test5", method = { RequestMethod.GET })
	public Map<String, Object> test5(User user) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("user", user);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/test6", method = { RequestMethod.GET })
	public Map<String, Object> test6(@RequestParam("id" ) Integer id, @RequestParam("name" ) String name) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("id", id);
		result.put("name", name);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/test7/{id}", method = { RequestMethod.GET })
	public Map<String, Object> test7(@PathVariable("id") Integer id) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("id", id);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/test8", method = { RequestMethod.GET })
	public Map<String, Object> test8(HttpServletRequest request) {
		Object val = request.getParameter("val");
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("val", val);
		return result;
	}
	
}